﻿/*
 *  PirateShare - a tool for fair&legal content sharing among friends.  
 *  see <http://pirateshare.codeplex.com> for latest version.
 *
 *  Copyright (C) 2010 Ilya Elenskiy
 *
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

using WPF_Dialogs;
using WPF_Dialogs.Dialogs;

namespace PirateShare
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int UPDATE_DELAY_MS = 1000; //update gui every second..

        DispatcherTimer WindowUpdateTimer;

        public ObservableCollection<LocalSharedFile> CurrentSharedFiles { get; set; }
        public ObservableCollection<string> CurrentSharedFolders { get; set; }

        public MainWindow()
        {
            CurrentSharedFiles = new ObservableCollection<LocalSharedFile>();
            CurrentSharedFolders = new ObservableCollection<string>();

            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WindowUpdateTimer = new DispatcherTimer(DispatcherPriority.Normal);
            WindowUpdateTimer.Tick += new EventHandler(WindowUpdateTimer_Tick);
            WindowUpdateTimer.Interval = TimeSpan.FromMilliseconds(UPDATE_DELAY_MS);
            WindowUpdateTimer.Start();
            
        }

        private void WindowUpdateTimer_Tick(object sender, EventArgs e)
        {
            if (Globals.indexWorkerThread.IsAlive)
            {
                this.txtReindexText.Text = "Stop index";
            }
            else
            {
                this.txtReindexText.Text = "Reindex";
            }

            if (!Enumerable.SequenceEqual<LocalSharedFile>(CurrentSharedFiles, Globals.ThisUser.SharedFiles))
            {
                CurrentSharedFiles.Clear();
                foreach (LocalSharedFile f in Globals.ThisUser.SharedFiles)
                {
                    CurrentSharedFiles.Add(f);
                }
            }

            if (!Enumerable.SequenceEqual<string>(CurrentSharedFolders, Globals.ThisUser.SharedFolders))
            {
                CurrentSharedFolders.Clear();
                foreach (string f in Globals.ThisUser.SharedFolders)
                {
                    CurrentSharedFolders.Add(f);
                }
            }

            //index worker status
            lock (Globals.indexWorker)
            {
                if (Globals.indexWorkerThread.IsAlive)
                {
                    if (Globals.indexWorker.Progress < 0)
                    {
                        pbIndex.IsIndeterminate = true;
                    }
                    else
                    {
                        pbIndex.IsIndeterminate = false;
                        pbIndex.Value = Globals.indexWorker.Progress;
                    }
                }
                else
                {
                    pbIndex.IsIndeterminate = false;
                    pbIndex.Value = 100;
                }
            }
        }


        #region Shared tab

        private void btnReindex_Click(object sender, RoutedEventArgs e)
        {
            if (Globals.indexWorkerThread.IsAlive)
            {
                lock (Globals.indexWorker)
                {
                    Globals.indexWorker.interruptWork = true;
                }
            }
            else
            {
                Globals.IndexWorkerTick(this, null); //force index update
                pbIndex.IsIndeterminate = true;
            }
            
        }

        private void btnAddFolder_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowseDialog folderDialog = new FolderBrowseDialog();
            EDialogResult res = folderDialog.showDialog();

            if (res == EDialogResult.OK)
            {
                string folder = folderDialog.SelectedPath;

                if (Globals.ThisUser.SharedFolders.Contains(folder)) { return; }

                Globals.ThisUser.SharedFolders.Add(folder);
                Globals.SaveUser();
                WindowUpdateTimer_Tick(null, null);
                btnReindex_Click(null, null);
            }
        }

        private void btnDelFolder_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.Yes != MessageBox.Show("Delete folder from share list - sure?", "Sure?", MessageBoxButton.YesNo, MessageBoxImage.Question))
            {
                return;
            }

            
            foreach (object f in lstSharedFolders.SelectedItems)
            {
                Globals.ThisUser.SharedFolders.Remove((string)f);
            }
        }



        private void lstSharedFilesHeader_Click(object sender, RoutedEventArgs e)
        {
            lstSharedFiles.Items.SortDescriptions.Clear();

            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction = ListSortDirection.Ascending;

            SortDescription resDescription = new SortDescription("Name", direction);

            switch ((string)headerClicked.Column.Header)
            {
                case "Filename":
                    resDescription = new SortDescription("Name", direction);
                    break;

                case "Type":
                    resDescription = new SortDescription("Type", direction);
                    break;

                case "Size":
                    resDescription = new SortDescription("Size", direction);
                    break;

                case "SHA-1":
                    resDescription = new SortDescription("Hash", direction);
                    break;


            }

            lstSharedFiles.Items.SortDescriptions.Add(resDescription);
            
        }


        #endregion

        


    }
}
